package com.lingx.web.api.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.lingx.model.impl.DefaultEntity;
import com.lingx.model.impl.DefaultMethod;
import com.lingx.service.LingxService;
import com.lingx.service.MethodService;
import com.lingx.service.ModelService;
import com.lingx.web.api.IApi;
@Component
public class Api9015 extends AbstractDevApi implements IApi{
	@Resource
	private ModelService modelService;
	@Resource
	private LingxService lingxService;
	@Resource
	private MethodService methodService;
	@Override
	public int getApiCode() {
		return 9015;
	}
	@Override
	public String getApiName() {
		return "获取对象功能树";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String code=IApi.getParamString(params,"code", "");
		List<String> checkedKeys=new ArrayList<String>();
		DefaultEntity entity=modelService.getEntity(code);
		List<Map<String,Object>> tree=new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> children=new ArrayList<Map<String,Object>>();
		Map<String,Object> root=new HashMap<String,Object>();
		int count=0;//lingxService.queryForInt("select count(*) from tlingx_func where module=?",code+"_object");
		root.put("text", entity.getName());
		root.put("expanded", true);//expanded: true
		root.put("checked", count>0);
	//if(count>0)checkedKeys.add(code);
		root.put("id",code);
		tree.add(root);
		root.put("children", children);
		for(DefaultMethod m:entity.getMethods().getList()){
			count=lingxService.queryForInt("select count(*) from tlingx_func where module=? and func=?",code,m.getCode());
			Map<String,Object> leaf=new HashMap<String,Object>();
			leaf.put("text", m.getName()+"["+m.getCode()+"]");
			leaf.put("id", m.getCode());
			leaf.put("leaf", true);
			leaf.put("checked", count>0);
			if(count>0)checkedKeys.add(m.getCode());
			children.add(leaf);
		}
		
		List<DefaultMethod> list=methodService.getDefaultMethods();
		for(DefaultMethod m: list){
			count=lingxService.queryForInt("select count(*) from tlingx_func where module=? and func=?",code,m.getCode());
			Map<String,Object> leaf=new HashMap<String,Object>();
			leaf.put("text",m.getName()+"["+m.getCode()+"]");
			leaf.put("id", m.getCode());
			leaf.put("leaf", true);
			leaf.put("checked", count>0);
			if(count>0)checkedKeys.add(m.getCode());
			children.add(leaf);
		}
		ret.put("data", tree);
		ret.put("checkedKeys", checkedKeys);
		
		return ret;
	}

}
